package com.xinqi.common.third.umeng.help;

import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.umeng.uapp.param.UmengUappGetAppCountParam;
import com.umeng.uapp.param.UmengUappGetAppCountResult;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.third.umeng.UmengProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * 友盟帮助类
 * <p>OpenApi文档： https://devs.umeng.com/api</p>
 * <p>OpenApi开通： https://developer.umeng.com/open-api/state</p>
 *
 * @author linkai
 * @date 2023/5/3
 */
@Slf4j
public class UmengHelp {
    private final UmengProperties umengProperties;
    private final ApiExecutor apiExecutor;

    public UmengHelp(UmengProperties umengProperties) {
        this.umengProperties = umengProperties;
        ApiExecutor executor = new ApiExecutor(umengProperties.getApiKey(), umengProperties.getApiSecurity());
        executor.setServerHost("gateway.open.umeng.com");
        this.apiExecutor = executor;
    }

    public ApiExecutor getApiExecutor() {
        return this.apiExecutor;
    }

    public String getApiKey() {
        return this.umengProperties.getApiKey();
    }

    /**
     * 获取App数量
     * <p><a href="https://developer.umeng.com/open-api/docs/com.umeng.uapp/umeng.uapp.getAppCount/1">获取App数量</a></p>
     */
    public UmengUappGetAppCountResult uAppGetAppCount(UmengUappGetAppCountParam param) {
        try {
            return this.apiExecutor.execute(param);
        } catch (OceanException e) {
            log.error("友盟接口调用失败 - uAppGetAppCount,  errorCode={}, errorMessage={}", e.getErrorCode(), e.getErrorMessage(), e);
            throw CommonErrors.BAD_REQUEST.asException("友盟接口调用失败");
        }
    }

}
